<?php

namespace app\index\controller;

use think\Controller;
use think\Db;//连接数据库类
use think\Request;
use app\index\model\Stu as Cla;
use think\Validate;
use app\index\model\Stu as checkStudent;
use app\index\controller\Common;

class Stu extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    // protected $pk='sid';
    // protected $table='stu';
    // public function banji()//跟班级表建立关系
    // {
    //     return $this->hasOne('Banji','cid','cid');
    // }
    public function index()
    {
        /*$cla = new Cla();
        $list = $cla->list();
        return $this->fetch("list",["list"=>$list]);*/

        $stu = new Cla();
        $list = $stu::select();
        $count=count($list);
        return view("index",["list"=>$list,"count"=>$count]);

    }
    // public function banji()
    // {

    // }
    public function add()
    {
    /*$data=[
    'name'=>input("param.name"),
    'sex'=>input("param.sex"),
    'name'=>input("param.name"),
        ]*/
        $cla = new Cla();
        if(input("?param.sub")){
        // if(!cla->check($data)){
            $file = request() -> file('files');
            if($file){
                $result = $cla->adds($file);
            }
            else
            {   
                $result = $cla->add();
            }
            // dump($cla->getError());
            if($result){
                $this->success("添加成功","index");
            }
            else{
                $this->error("添加失败");
            }
        }
        else{
            return view("add");
            // $result = Db::name('student')->insert($data);
        }

    }

   public function delete()
	{
		$id = input("param.id");
		$cla = new Cla();
		$result = $cla->deleteById($id);
		if($result){
			$this->success("删除成功","stu/index");
		}else{
				$this->error("删除失败");
			}
	}


    public function updat($id)
    {
        $list = Db::table('login')->select();
        $stu = new Cla();
        if(request()->isPost()){
            $result=$stu->edit($id);
            if($result){
                $this->success("修改成功",'index');
            }else{
                $this->success("修改失败");
            }
        }else{
            $data = Cla::find($id);

            return view("edit",["data"=>$data,'cate'=>$list]);
        }
    }
        public function select()
        {
            // echo input("param.name");die();
            if (input("?param.sub")) {
                $cla = new Cla();
               
                $list = $cla->mh(input('param.name'));
                return view('index', ['list' => $list]);
            }

        }
        public function getSession()
    {
        if(Session::has('username')){
            echo Session::get("username");
        }else{
            echo "用户没有登录";
        }
    }

    public function delSession()
    {
        //Sessjion::delete("username");
        //清除session(当前作用域)
        Session::clear();
    }
    //图片上传
    // public function up(Request $request)
    // {
    //     $file = $request -> file('file2');
    //     print_r($file);
    //     exit;
    //     $result = $this->validate(['file2' => $file],['file2' => 'require|image'],['file2.require'  => '请选择上传文件','file2.image' => '非法图片上传']);
    //     if(true !== $result){
    //         $this -> error($result);
    //     }
    //     $info = $file->rule('date')->move(ROOT_PATH . 'piblic' . DS . 'uploads');
    // }

    // public function login()
    // {
    //     if(input("?param.sub")){
    //         $validate = input("param.validate");
    //         if(captcha_check($validate)){
    //             $admin = new Admin();
    //             $result = $admin->getAdmin();
    //             if($result){
    //                 session("username",$result['username']);
    //                 $this->redirect("stu/index");
    //             }else{
    //                 $this->error("用户名或密码错误，请重新输入");
    //             }else{
    //                 $this->error("验证码错误，请从新输入");
    //             }
    //         }
    //     }
    // }
}
